﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SelectApprover.aspx.cs" Inherits="YBApproval.Pages.SelectApprover" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="/Scripts/jquery-1.9.1.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <input type="hidden" id="hdIsRole" />
        <input id="txtKeyword" type="text" style="width:285px" />
        <input id="btnSearch" type="button" value="查找" onclick="selectApprover();"/>
        <br />        
        <div id="type_div" style="display:none">
        <input type="radio" id="rdUser" name="type" value="User" onclick="getUserOrRole()" checked="checked" />用户
        <input type="radio" id="rdRole" name="type" value="Role" onclick="getUserOrRole()" />角色
        </div>
        <select size="4" name="lbApprovers" id="lbApprovers" ondblclick="lbOnDoubleClick();" style="height:206px;width:335px;">
        </select>    
    </div>
    </form>
</body>
</html>

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        if (getQueryStringByName("type") == "role") {
            $("#type_div").show();
        }
        else {
            $("#type_div").hide();
        }
        getUserOrRole();
    })
    function getQueryStringByName(name) {
        var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
        if (result==null||result.length<1) {
            return "";
        }
        return result[1];
    }
    function getUserOrRole() {
        var type = $('input:radio[name="type"]:checked').val();
        if (type == "User") {
            $("#hdIsRole").val("false");
            $.get('/Pages/Services.aspx?Action=GetUsers', function (data) { $("#lbApprovers").html(data) });
        }
        else {
            $("#hdIsRole").val("true");
            $.get('/Pages/Services.aspx?Action=GetRoles', function (data) { $("#lbApprovers").html(data) });
        }
    }



    document.onkeydown = function (event) {
        if (event.keyCode == 13) {
            event.keyCode = 0;
            event.returnValue = false;
        }
//        if (event.keyCode == 13 && event.srcElement.type == "textarea") {
//            event.returnValue = true;
//        }
//        else {
//            event.returnValue = false;
//        }
    }

//        onkeydown = "keywordOnKeyDown(event)"
//    function keywordOnKeyDown(e) {
//        var keynum = -1;  // initial value
//        
//        if (window.event) // IE
//        {
//            keynum = e.keyCode
//        }
//        else if (e.which) // Netscape/Firefox/Opera
//        {
//            keynum = e.which
//        }

//        if (keynum == 13) {
//            selectApprover();
//        }
//    }

    function selectApprover() {
        var keyword = document.getElementById("txtKeyword").value;
        if (keyword != null && keyword != undefined && keyword != "") {
            var approvers = document.getElementById("lbApprovers");
            for (var i = 0; i < approvers.options.length; i++) {
                if (approvers.options[i].innerText.indexOf(keyword) == 0) {
                    approvers[i].selected = true;
                    break;
                }
            }
        }
    }

    function lbOnDoubleClick() {
        var approvers = document.getElementById("lbApprovers");
        var type = $("#hdIsRole").val();
        for (var i = 0; i < approvers.options.length; i++) {
            if (approvers[i].selected == true) {
                window.returnValue = type + "|||" + approvers.options[i].innerText + "|||" + approvers.options[i].value;
                break;
            }
        }        
        window.close();
    }
</script>